package com.psp.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.northend.util.StringUtil;
import com.psp.web.model.UserMsgBean;
import com.psp.web.persist.dao.UserMsgDao;
import com.psp.web.service.MsgService;
import com.psp.web.service.exception.ServiceException;

@Service
public class MsgServiceImpl implements MsgService {

	@Autowired
	UserMsgDao userMsgImpl;

	@Override
	public PageResult<UserMsgBean> getMsgs(int page, int pageSize, String uid, Integer type, Integer msgType) {
		PageResult<UserMsgBean> result = new PageResult<>();
		int count = userMsgImpl.selectCount(null, uid, type, msgType);
		List<UserMsgBean> list = userMsgImpl.selectList(page, pageSize, null, uid, type, msgType);
		result.setCount(count);
		result.setData(list);
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean del(String uid, Long id) throws ServiceException {
		boolean flag = false;
		if (StringUtil.isEmpty(uid)) {
			throw new ServiceException("error", "用户不存在");
		}
		UserMsgBean userMsg = userMsgImpl.selectById(id);
		if (userMsg == null) {
			throw new ServiceException("object_is_not_exist", "消息");
		}

		if (!uid.equals(userMsg.getTid())) {
			throw new ServiceException("error", "不是该用户的消息");
		}
		flag = userMsgImpl.delete(id) > 0;
		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

}
